package com.mj.mafeed.adapter;

import java.util.List;

import com.mj.mafeed.R;
import com.mj.mafeed.bean.OrderJSON;
import com.mj.mafeed.bean.OrderJSON.OrderItem;
import com.mj.mafeed.util.Utility;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 订单列表
 * @author zhaominglei
 * @date 2014-12-16
 * 
 */
public class OrderAdapter extends BaseAdapter {
	private List<OrderJSON> orders;
	private Context context;
	private ListView orderItemView;
	private OrderItemAdapter orderItemAdapter;
	private List<OrderItem> orderItems;
	
	public OrderAdapter(Context context, List<OrderJSON> orders) {
		super();
		this.context = context;
		this.orders = orders;
	}

	public List<OrderJSON> getOrders() {
		return orders;
	}

	public void setOrders(List<OrderJSON> orders) {
		this.orders = orders;
	}
	
	@Override
	public int getCount() {
		return orders.size();
	}

	@Override
	public Object getItem(int position) {
		return orders.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LinearLayout relativeLayout;
		if (convertView != null) {
			relativeLayout = (LinearLayout) convertView;
		} else {
			relativeLayout = (LinearLayout) View.inflate(context,
					R.layout.order_item, null);
		}
		orderItemView = (ListView) relativeLayout.findViewById(R.id.order_item_listview);
		orderItems = orders.get(position).getOrderItems();
		orderItemAdapter = new OrderItemAdapter(relativeLayout.getContext(), orderItems);
		orderItemView.setAdapter(orderItemAdapter);
		new Utility().setListViewHeightBasedOnChildren(orderItemView);
		((TextView) relativeLayout.findViewById(R.id.order_list_template_status))
				.setText(String.valueOf(orders.get(position).getOrderStatus()));
		((TextView) relativeLayout.findViewById(R.id.order_list_template_id))
				.setText(String.valueOf(orders.get(position).getOrderId()));
		((TextView) relativeLayout.findViewById(R.id.order_list_template_sn))
				.setText(orders.get(position).getSn());
		((TextView) relativeLayout.findViewById(R.id.order_list_template_amount))
				.setText(String.valueOf(orders.get(position).getAmount()));
		return relativeLayout;
	}
}
